<?php /*
	*/ ?>
<?php
include("system/config.inc.php");
//insert into customer basket
if(isset($_REQUEST['proid']) && ($_REQUEST['proid'])!="") {
	if(isset($_REQUEST['action']) && ($_REQUEST['action'])=="2") {
		$session=session_id();
		$seid=mysql_real_escape_string($_REQUEST['proid']);
		$ip=$_SERVER['REMOTE_ADDR'];
		$qty="1";
			if(isset($_SESSION['memberid']) && ($_SESSION['memberid'])!="")	{
				$memid=$_SESSION['memberid'];
			} else {
				$memid=0;
			}
		//get item base price
		$sqlsel="select * from tblproducts where intprodid='$seid'";
		$respr=$n02586916_db->select($sqlsel);
			if(count($respr)>0) {
				$price=$respr[0]['decprice'];
			}
		// insert into customer basket primary submit	
		$sql = "INSERT INTO `tblcustomerbaskets` (`varsessionid`,`varip`,`intcustomerid`,`intproductid`,`intproductquantity`,`dectotalprice`,`dtdateadded`) 
					VALUES ('$session','$ip','$memid','$seid','$qty','$price',NOW())";
		$insert=$n02586916_db->insert($sql);
		// get basket id for attribute insert
		$basketid=mysql_insert_id($n02586916_db->CONN);
			
		// pull attribute fields from details page
		$totoptions = mysql_real_escape_string($_POST['totoptions']);
			$optarray = array();
			for($a=1;$a<=$totoptions;$a++) {
				// insert into attributes table attribute id
				$optvalid = mysql_real_escape_string($_POST['prodoptions'.$a]);
				$optarray[] = $optvalid ;
				$sql3="INSERT INTO `tblcustmerbasketattributes` (`intcustomerbasketid`,`intproductoptionid`) VALUES ('$basketid','$optvalid')";
				$insert=$n02586916_db->insert($sql3);	
			}

		//pull attribute price effectors to calculate new total price for item
		for ($b=0;$b<count($optarray);$b++) {
			$pricesql="SELECT decprice, charprefix FROM tblproductattributes WHERE intattributeid=".$optarray[$b];
			$priceresults=$n02586916_db->select($pricesql);
			for($c=0;$c<count($priceresults);$c++) {
				if ($priceresults[$c]['charprefix']=="+") {
					$price = $price + $priceresults[$c]['decprice'];
				} else {
					$price = $price - $priceresults[$c]['decprice'];
				}	 
			}
		}
		// update basket with total item price for this item including effectors
		$sql=mysql_query("UPDATE tblcustomerbaskets SET dectotalprice=$price where intid=$basketid");
		//redirect user to basket
		header("location:cart.php?proid=".$seid);
		die();			
	}	
}
?>
